Skip to content

Support more converter forms in the attrs plugin#21408

Open
filbranden wants to merge 1 commit intopython:masterfrom
filbranden:converters1
Open

Support more converter forms in the attrs plugin#21408
filbranden wants to merge 1 commit intopython:masterfrom
filbranden:converters1

Conversation

@filbranden
Copy link
Copy Markdown

The attrs plugin only recognized converters that were named functions, types, or lambdas. Extend it to also accept:

The init parameter type is still derived from the first positional parameter of the wrapped callable, so existing diagnostics for converters with the wrong signature continue to apply.

Test stubs grew a non-generic Converter class plus pipe / to_bool so the new behavior can be exercised. The field's converter= parameter now accepts Callable | Converter via a new _FieldConverterType alias, which slightly changes the diagnostic message in testAttrsUsingBadConverter[Reprocess].

Fixes #15736.

This was authored with the help of Claude Code.

NOTE: Per test-data/unit/README.md, lib-stub expansion should be avoided and is the kind of change reviewers may want to discuss. Here I have only added the Converter class, _FieldConverterType alias and pipe and to_bool converter functions. These are necessary to test the new converter forms and, as such, I imagine they might be acceptable. Let me know if otherwise.

Checklist:

  • Read the Contributing Guidelines
  • Add tests for all changed behaviour.
  • N/A: If you can't add a test, please explain why and how you verified your changes work.
  • Make sure CI passes.
  • Please do not force push to the PR once it has been reviewed.

The attrs plugin only recognized converters that were named functions,
types, or lambdas. Extend it to also accept:

- Calls returning a callable, e.g. `converter=make_converter(arg)` — the
  long-standing request in python#15736.
- Variables annotated with a callable type.
- The `attrs.Converter` wrapper class, including the `takes_self` and
  `takes_field` keyword forms.
- `attrs.converters.pipe(...)` — the existing `Overloaded` handling was
  too strict to recognize its return type.

The init parameter type is still derived from the first positional
parameter of the wrapped callable, so existing diagnostics for
converters with the wrong signature continue to apply.

Test stubs grew a non-generic `Converter` class plus `pipe` / `to_bool`
so the new behavior can be exercised. The field's `converter=` parameter
now accepts `Callable | Converter` via a new `_FieldConverterType`
alias, which slightly changes the diagnostic message in
`testAttrsUsingBadConverter[Reprocess]`.

Fixes python#15736.

This was authored with the help of Claude Code.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/helpers/entity_registry.py:223: error: Unused "type: ignore" comment  [unused-ignore]

@filbranden
Copy link
Copy Markdown
Author

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/helpers/entity_registry.py:223: error: Unused "type: ignore" comment  [unused-ignore]

Nice! This is exactly the issue to fix. If this PR goes in and a new release of mypy includes it, I'm glad to follow up with a PR to that project to drop the no longer needed "type: ignore", together with a bump of the mypy version in use there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsupported converter, only named functions, types and lambdas are currently supported with higher-order function

1 participant